package test;

import java.awt.peer.CanvasPeer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class test3 {
    public static void main(String[] args) {
        int n,m;//n表示序列数，m表示操作次数
        Scanner scanner=new Scanner(System.in);
        n=scanner.nextInt();//序列数
        m=scanner.nextInt();//操作次数
        ArrayList<Integer> list=new ArrayList<>();//序列(数组)
        for (int i=1;i<n;i++){
            list.add(scanner.nextInt());
        }

//        System.out.println(getMax(list));
//        System.out.println(getMin(list));
        ArrayList<ArrayList> lists = chooseUtils(m);
        for (ArrayList list1:lists
             ) {
            for (Object choose:list1
                 ) {
                modifyNum(list, (Integer) choose);
            }
        }
        int re = getMax(list) - getMin(list);
        System.out.println(re);
    }
    public static int getMax(ArrayList<Integer> list){
        return Collections.max(list);
    }
    public static int getMin(ArrayList<Integer> list){
        return Collections.min(list);
    }
    //choose控制器
    public static ArrayList<ArrayList> chooseUtils(int m){
        int x=m,y=0;//x管理choose=1，y管理choose=2；
        int choose1=1,choose2=2;
        ArrayList chooseLists=new ArrayList<>();
        while(y!=m){
            ArrayList<Integer> chooseList=new ArrayList<>();
            for (int i=1;i<=m-y;i--){
                chooseList.add(choose1);
            }
            for (int j=1;j<=y;j++){
                chooseList.add(choose2);
            }
            chooseLists.add(chooseList);
        }
        return chooseLists;
    }
    //修改数据
    public static ArrayList modifyNum(ArrayList<Integer> list,int choose){
        int max=getMax(list);
        int min=getMin(list);
        if (choose==1){
            //最大值改变为最小值
            for (int num:list) {
                if (num==max){
                    list.remove(num);
                    num=min;
                    list.add(num);
                }
            }
        }
        if (choose==2){
            //最小值改变为最大值
            for (int num:list) {
                if (num==min){
                    list.remove(num);
                    num=max;
                    list.add(num);
                }
            }
        }return list;
    }
}
